/**
* 显示消息提示框
* @param content 提示的标题
*/
export function toast(content) {
  uni.showToast({
    icon: 'none',
    title: content
  })
}

/**
* 显示模态弹窗
* @param content 提示的标题
*/
export function showConfirm(content) {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title: '提示',
      content: content,
      cancelText: '取消',
      confirmText: '确定',
      success: function(res) {
        resolve(res)
      }
    })
  })
}

/**
* 参数处理
* @param params 参数
*/
export function tansParams(params) {
  let result = ''
  for (const propName of Object.keys(params)) {
    const value = params[propName]
    var part = encodeURIComponent(propName) + "="
    if (value !== null && value !== "" && typeof (value) !== "undefined") {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
            let params = propName + '[' + key + ']'
            var subPart = encodeURIComponent(params) + "="
            result += subPart + encodeURIComponent(value[key]) + "&"
          }
        }
      } else {
        result += part + encodeURIComponent(value) + "&"
      }
    }
  }
  return result
}

// 生成UUID
export const genUUID = () => {
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
    const r = (Math.random() * 16) | 0
    const v = c === "x" ? r : (r & 0x3) | 0x8
    return v.toString(16)
  })
}

// 生成随机字符串
export const genRandomString = (length = 6) => {
  let result = '';
  const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
  const charactersLength = characters.length;
  for (let i = 0; i < length; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
  }
  return result;
}

// 生成随机频道名
export const getRandomChannel = (number = 6) => {
  return "agora_" + genRandomString(number)
}

// 生成用户ID
export const genUserUID = () => {
  return Math.floor(Math.random() * 99999) + 100000
}

/**
 * 将对象中的空字符串转换为null
 * @param {Object} obj - 要处理的对象
 * @returns {Object} - 处理后的对象
 */
export function convertEmptyToNull(obj) {
  if (!obj || typeof obj !== 'object') {
    return obj;
  }
  
  const result = {};
  for (const [key, value] of Object.entries(obj)) {
    if (value === '') {
      result[key] = null;
    } else if (typeof value === 'object' && value !== null) {
      result[key] = convertEmptyToNull(value);
    } else {
      result[key] = value;
    }
  }
  
  return result;
}

/**
 * 将对象中的空字符串和undefined转换为null
 * @param {Object} obj - 要处理的对象
 * @returns {Object} - 处理后的对象
 */
export function convertEmptyAndUndefinedToNull(obj) {
  if (!obj || typeof obj !== 'object') {
    return obj;
  }
  
  // 保持数组类型不变
  if (Array.isArray(obj)) {
    return obj.map((item) => {
      if (item === '' || item === undefined) return null;
      if (item && typeof item === 'object') return convertEmptyAndUndefinedToNull(item);
      return item;
    });
  }
  
  const result = {};
  for (const [key, value] of Object.entries(obj)) {
    if (value === '' || value === undefined) {
      result[key] = null;
    } else if (typeof value === 'object' && value !== null) {
      result[key] = convertEmptyAndUndefinedToNull(value);
    } else {
      result[key] = value;
    }
  }
  
  return result;
}

/**
 * 测试空值转换功能
 * 用于验证convertEmptyAndUndefinedToNull函数是否正常工作
 */
export function testEmptyToNullConversion() {
  const testData = {
    name: '张三',
    age: '',
    email: undefined,
    address: '北京市',
    phone: '',
    hobbies: ['读书', '', '游泳'],
    profile: {
      bio: '',
      website: undefined,
      location: '上海'
    }
  };
  
  const result = convertEmptyAndUndefinedToNull(testData);
  
  console.log('原始数据:', testData);
  console.log('转换后数据:', result);
  
  // 验证转换结果
  const expected = {
    name: '张三',
    age: null,
    email: null,
    address: '北京市',
    phone: null,
    hobbies: ['读书', null, '游泳'],
    profile: {
      bio: null,
      website: null,
      location: '上海'
    }
  };
  
  console.log('转换是否正确:', JSON.stringify(result) === JSON.stringify(expected));
  
  return result;
}